package com.example.javaairportflightanalysis.monitor;

import com.example.javaairportflightanalysis.dao.DfdlMapper;
import com.example.javaairportflightanalysis.domin.dto.DfdlDto;
import com.example.javaairportflightanalysis.service.DfdlService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ListMonitorTask {
    @Resource
    private DfdlMapper dfdlMapper;
    @Resource
    private DfdlService dfdlService;

    private  Integer dfdlAListSize=7950;
    private  Integer dfdlDListSize=7286;

    private ArrayList<DfdlDto> dfdlDtoMerges = new ArrayList<>();

    @Scheduled(fixedRate = 5000) // 每5秒执行一次任务
    public void insertDataToDatabaseA2() {
        ArrayList<DfdlDto> d2401 = dfdlMapper.getA2401();
        int newSize = d2401.size();

        log.info("insertDataToDatabaseA2 newSize:{}",newSize);
        if (newSize > dfdlAListSize) {
            // 获取新的数据，并将其添加到 dfdlDtoMerges 集合中
            List<DfdlDto> newData = d2401.subList(dfdlDtoMerges.size(), newSize);
            dfdlDtoMerges.addAll(newData);
            try {
                dfdlService.insertAApno2(dfdlDtoMerges);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            log.info("insertDataToDatabaseA2 有新数据插入");
            dfdlAListSize=newSize;
            dfdlDtoMerges.clear();
        }
    }

    @Scheduled(fixedRate = 5000) // 每5秒执行一次任务
    public void insertDataToDatabaseA3() throws ParseException {
        ArrayList<DfdlDto> d2401 = dfdlMapper.getA2401();
        int newSize = d2401.size();

        if (newSize > dfdlAListSize) {
            // 获取新的数据，并将其添加到 dfdlDtoMerges 集合中
            List<DfdlDto> newData = d2401.subList(dfdlDtoMerges.size(), newSize);
            dfdlDtoMerges.addAll(newData);
            dfdlService.insertAApno3(dfdlDtoMerges);
            log.info("insertDataToDatabaseA3");
            dfdlAListSize=newSize;
            dfdlDtoMerges.clear();
        }
    }

    @Scheduled(fixedRate = 5000) // 每5秒执行一次任务
    public void insertDataToDatabaseD2() {

        ArrayList<DfdlDto> d2401 = dfdlMapper.getD2401();
        int newSize = d2401.size();
        log.info("insertDataToDatabaseD2 newSize:{}",newSize);
        if (newSize > dfdlDListSize) {
            // 获取新的数据，并将其添加到 dfdlDtoMerges 集合中
            List<DfdlDto> newData = d2401.subList(dfdlDtoMerges.size(), newSize);
            dfdlDtoMerges.addAll(newData);
            try {
                dfdlService.insertDApno2(dfdlDtoMerges);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            log.info("insertDataToDatabaseD2 有新数据插入" );
            dfdlDListSize=newSize;
            dfdlDtoMerges.clear();
        }
    }

    @Scheduled(fixedRate = 5000) // 每5秒执行一次任务
    public void insertDataToDatabaseD3() {

        ArrayList<DfdlDto> d2401 = dfdlMapper.getD2401();
        int newSize = d2401.size();

        if (newSize >dfdlDListSize) {
            // 获取新的数据，并将其添加到 dfdlDtoMerges 集合中
            List<DfdlDto> newData = d2401.subList(dfdlDtoMerges.size(), newSize);
            dfdlDtoMerges.addAll(newData);
            try {
                dfdlService.insertDApno3(dfdlDtoMerges);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            log.info("insertDataToDatabaseD3 有新数据插入");
            dfdlDListSize=newSize;
            dfdlDtoMerges.clear();
        }
    }
}